package com.revzone.demo001.PermissionModule.controller;
import com.revzone.demo001.PermissionModule.service.RoleService;
import com.revzone.demo001.entity.OtherEntity.R;
import com.revzone.demo001.entity.PermissionEntity.RoleDTO;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *  前端控制器
 * @author lsa
 * @since 2023-03-27
 */
@RestController
@Api(tags = "角色role管理", description = "用于角色role管理的相关API")
@RequestMapping("/role")
public class RoleController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    RoleService roleService;

    @PostMapping(value="/getRoleListPage", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R getRoleListPage(@RequestBody(required = false) RoleDTO roleDTO) {
        logger.info("欢迎来到角色分页查询接口!");
        if (roleDTO == null) roleDTO = new RoleDTO();
        return roleService.getRoleListPage(roleDTO);
    }

    @PostMapping(value="/getRoleList", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R getRoleList(@RequestBody(required = false) RoleDTO roleDTO) {
        logger.info("欢迎来到角色查询接口!");
        if (roleDTO == null) roleDTO = new RoleDTO();
        return roleService.getRoleList(roleDTO);
    }


    @PostMapping(value="/setRoleList", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R setRoleList(@RequestBody(required = false) RoleDTO roleDTO) {
        logger.info("欢迎来到角色新增接口!");
        if (roleDTO == null) roleDTO = new RoleDTO();

        return roleService.setUserList(roleDTO);
    }

    @PostMapping(value="/updateRoleList", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R updateRoleList(@RequestBody(required = false) RoleDTO roleDTO) {
        logger.info("欢迎来到角色修改接口!");
        if (roleDTO == null) roleDTO = new RoleDTO();
        return roleService.updateUserList(roleDTO);
    }

    @GetMapping(value="/delRoleList", produces = "application/json;charset=UTF-8")
    @RequiresRoles(logical = Logical.OR, value = {"root","user", "admin"})
    public R delRoleList(@RequestParam(required = false,defaultValue = "0",value = "id") Integer id) {
        logger.info("欢迎来到角色删除接口!");
        return roleService.delUserList(id);
    }
}
